package com.admin.system.controller;

import com.admin.model.base.PageResult;
import com.admin.model.dto.UserDto;
import com.admin.model.pojo.Role;
import com.admin.model.dto.RoleDto;
import com.admin.system.service.RoleService;
import com.admin.system.service.mapstruct.RoleMapper;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import javax.validation.groups.Default;
import java.util.List;
import java.util.Set;

/**
 * (Role)表控制层
 *
 * @author makejava
 * @since 2021-01-31 19:58:51
 */
@Api(tags = "角色")
@Validated
@RestController
@RequestMapping("role")
public class RoleController {
    @Resource
    private RoleService roleService;


    @ApiOperation("创建")
    @ApiOperationSupport(ignoreParameters = {"id", "menus", "createTime", "updateTime", "createdBy", "updatedBy"})
    @PreAuthorize("hasAnyAuthority('role:add')")
    @PostMapping
    public ResponseEntity<Object> create(@Validated({RoleDto.Create.class, Default.class}) @RequestBody RoleDto roleDto) {
        roleService.create(roleDto);
        return ResponseEntity.ok("创建成功");
    }

    @ApiOperation("id查询")
    @PreAuthorize("hasAnyAuthority('role:list')")
    @GetMapping("/{id}")
    public ResponseEntity<Object> findById(@PathVariable Long id) {
        return ResponseEntity.ok(roleService.queryById(id));
    }

    @ApiOperation("角色下拉列表")
    @GetMapping("/list")
    public ResponseEntity<Object> findList() {
        return ResponseEntity.ok(roleService.findList());
    }

    @ApiOperation("授权菜单")
    // @PreAuthorize("hasAnyAuthority('role:edit')")
    @PostMapping("/auth")
    public ResponseEntity<String> authMenus(@NotEmpty(message = "菜单id不能为空") @RequestBody List<Long> menuIds,
                                            @NotEmpty(message = "角色id不能为空") @RequestParam Long roleId) {
        roleService.authMenus(roleId, menuIds);
        return ResponseEntity.ok("授权成功");
    }

    @ApiOperation("ids删除")
    @PreAuthorize("hasAnyAuthority('role:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteByIds(@RequestBody Set<Long> ids) {
        roleService.deleteByIds(ids);
        return ResponseEntity.ok("删除成功");
    }

    @ApiOperation("条件查询")
    @ApiOperationSupport(ignoreParameters = {"menus"})
    @PreAuthorize("hasAnyAuthority('role:list')")
    @PostMapping("/search")
    public ResponseEntity<Object> findAll(@RequestParam(defaultValue = "1") int pageNum,
                                          @RequestParam(defaultValue = "10") int pageSize,
                                          @RequestParam(defaultValue = "-create_time") String sort,
                                          @RequestBody(required = false) RoleDto roleDto) {
        PageResult pageResult = roleService.queryAll(roleDto, pageNum, pageSize, sort);
        return ResponseEntity.ok(pageResult);
    }

    @ApiOperation("角色查询用户")
    @GetMapping("/role/{roleId}")
    public ResponseEntity<Object> findUserByRoleId(@PathVariable("roleId") @JsonSerialize(using = ToStringSerializer.class) Long roleId) {
        List<UserDto> userDtos = roleService.queryUserByRoleId(roleId);
        return ResponseEntity.ok(userDtos);
    }

    @ApiOperation("id更新")
    @ApiOperationSupport(ignoreParameters = {"menus", "createTime", "updateTime", "createdBy", "updatedBy"})
    // @PreAuthorize("hasAnyAuthority('role:edit')")
    @PutMapping
    public ResponseEntity<Object> updateById(@Validated({RoleDto.Update.class, Default.class}) @RequestBody RoleDto roleDto) {
        roleService.updateById(roleDto);
        return ResponseEntity.ok("更新成功");
    }


}